<?php
include('../../includes/classes/core.php');
 
$user_id		= $_SESSION['USERID'];
$warehouse_id	= $_REQUEST['id'];
$page = '(1/1)';

$rResult = mysql_fetch_array(mysql_query("	SELECT		`warehouse`.`id` as `id`,
														CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `client` ,
														`warehouse`.`end_date`
											FROM		`warehouse`
											LEFT JOIN	`client_objects`	ON `client_objects`.`id` = `warehouse`.`client_object_id`
											LEFT JOIN	`client` ON `client`.`id` = `client_objects`.`client_id`
											WHERE		`warehouse`.`id` = '$warehouse_id'"));

$rResult1 = mysql_fetch_array(mysql_query("SELECT		`persons`.`name` as `name`
											FROM		persons
											LEFT JOIN	users ON users.person_id = persons.id
											WHERE  		users.id = $user_id"));


function print1($rResult){
	GLOBAL $warehouse_id;
	GLOBAL $page;
	$data = '';
	$rsQl = mysql_query("SELECT		warehouse_detail.barcode,
									production.`name`,
									CASE WHEN ISNULL(part2.id) THEN 0
										WHEN NOT ISNULL(part2.id) THEN 1
									END AS 'ტონერი',
									CASE WHEN ISNULL(part3.id) THEN 0
										WHEN NOT ISNULL(part3.id) THEN 1
									END AS 'ბარაბანი',
									CASE WHEN ISNULL(part4.id) THEN 0
										WHEN NOT ISNULL(part4.id) THEN 1
									END AS 'მაგნიტური ლილვი',
									CASE WHEN ISNULL(part5.id) THEN 0
										WHEN NOT ISNULL(part5.id) THEN 1
									END AS 'რეზინის ლილვი',
									CASE WHEN ISNULL(part6.id) THEN 0
										WHEN NOT ISNULL(part6.id) THEN 1
									END AS 'გამწ. რაკელი',
									CASE WHEN ISNULL(part7.id) THEN 0
										WHEN NOT ISNULL(part7.id) THEN 1
									END AS 'დოზ. რაკელი',
									CASE WHEN ISNULL(part8.id) THEN 0
										WHEN NOT ISNULL(part8.id) THEN 1
									END AS 'მიკრო ჩიპი',
									CASE WHEN ISNULL(part9.id) THEN 0
										WHEN NOT ISNULL(part9.id) THEN 1
									END AS 'კორპუსი',
	                               	CASE WHEN ISNULL(part10.id) THEN 0
										WHEN NOT ISNULL(part10.id) THEN 1
									END AS 'სერვისის გარეშე',
									`persons`.`name`
					FROM  		warehouse_producing
					JOIN 	warehouse_detail ON warehouse_detail.id = warehouse_producing.warehouse_detail_id
					LEFT JOIN 	warehouse ON warehouse.id = warehouse_detail.warehouse_id
					LEFT JOIN 	production ON production.id = warehouse_detail.prod_id
					LEFT JOIN 	client_objects ON client_objects.id = warehouse.client_object_id
					LEFT JOIN 	client ON client.id = client_objects.client_id
					LEFT JOIN 	cartridge_pricelist ON client.id = cartridge_pricelist.client_id AND warehouse_producing.cartridge_part_id = cartridge_pricelist.parts_type AND cartridge_pricelist.production_id = warehouse_detail.prod_id
					LEFT JOIN 	warehouse_producing AS part2 ON part2.cartridge_part_id = 2 AND part2.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part3 ON part3.cartridge_part_id = 3 AND part3.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part4 ON part4.cartridge_part_id = 4 AND part4.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part5 ON part5.cartridge_part_id = 5 AND part5.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part6 ON part6.cartridge_part_id = 6 AND part6.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part7 ON part7.cartridge_part_id = 7 AND part7.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part8 ON part8.cartridge_part_id = 8 AND part8.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part9 ON part8.cartridge_part_id = 9 AND part9.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part1 ON part2.cartridge_part_id = 1 AND part1.warehouse_detail_id = warehouse_detail.id
					LEFT JOIN 	warehouse_producing AS part10 ON part10.cartridge_part_id = 16 AND part10.warehouse_detail_id = warehouse_detail.id
	               LEFT JOIN		persons ON persons.id = warehouse_producing.person_id
					WHERE 			warehouse.id = '$warehouse_id' AND warehouse_detail.actived=1
					GROUP BY 		warehouse_detail.id
					ORDER BY  		warehouse_detail.id   ");
	if( mysql_num_rows($rsQl) <= 38 ){
		for($j=1;$j<=38;$j++){
			while( $rezsQl = mysql_fetch_array($rsQl) ){
				$data .= '<tr>';
				$data .= '<td>' . $j . '</td>';
				for ( $i = 0 ; $i < 12 ; $i++ ){
					if($i< 2 || $i > 10 ){
						$data .='<td>'.$rezsQl[$i].'</td>';
					}else{
						if($rezsQl[$i] == 1){
							$data .= '<td><img src="../../media/images/0.gif"></td>';
						}else{
							$data .= '<td>&nbsp;</td>';
						}
					}
				}
				$data .= '</tr>';
				$j++;
			}
			$data .='
				<tr>
					<td>' . $j . '</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>    
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			';
		}


		$data .='
		</table>
		<div style="margin-top: 80px; margin-left: 40px; float: left;">
		სს " '. $rResult['client'] .' " შეკვეთა № :&nbsp;'.'R –  '. $rResult['id'] .' &nbsp;'. $rResult['planned_end_date'] .'
		</div>
		<div style="margin-top: 80px; float: right;">
			'. $page .'
		</div>';


	}else if( mysql_num_rows($rsQl) <= 88 ){
		$page = '(1/2)';
		$j=1;
		for($j=1;$j<=38;$j++){
			$rezsQl = mysql_fetch_array($rsQl);
			$data .= '<tr>';
			$data .= '<td>' . ($j) . '</td>';
			for ( $i = 0 ; $i < 12 ; $i++ ){
				if($i< 2 || $i > 10 ){
					$data .='<td>'.$rezsQl[$i].'</td>';
				}else{
					if($rezsQl[$i] == 1){
						$data .= '<td><img src="../../media/images/0.gif"></td>';
					}else{
						$data .= '<td>&nbsp;</td>';
					}
				}
			}
			$data .= '</tr>';
		}
		$data .='
		</table>
		<div style="margin-top: 80px; margin-left: 40px; float: left;">
		სს " '. $rResult['client'] .' " შეკვეთა № :&nbsp;'. $rResult['id'] .' &nbsp;'. $rResult['planned_end_date'] .'
		</div>
		<div style="margin-top: 80px; float: right; padding-bottom: 100px;">
			'. $page .'
		</div>';
		$page = '(2/2)';

		$data .='<table border="1" class="job-blank" cellpadding="0" cellspacing="0">';
		for( ;$j<= 88;$j++){
			while( $rezsQl = mysql_fetch_array($rsQl) ){
				$data .= '<tr>';
				$data .= '<td>' . $j . '</td>';
				for ( $i = 0 ; $i < 12 ; $i++ ){
					if($i< 2 || $i > 10 ){
						$data .='<td>'.$rezsQl[$i].'</td>';
					}else{
						if($rezsQl[$i] == 1){
							$data .= '<td><img src="../../media/images/0.gif"></td>';
						}else{
							$data .= '<td>&nbsp;</td>';
						}
					}
				}
				$data .= '</tr>';
				$j++;
			}

		}
		$data .='
		</table>
		<div style="margin-top: 80px; margin-left: 40px; float: left; vertical-align:bottom;">
		სს " '. $rResult['client'] .' " შეკვეთა № : '.'R –  '. $rResult['id'] .' &nbsp;'. $rResult['planned_end_date'] .'
		</div>
		<div style="margin-top: 80px; float: right;">
			'. $page .'
		</div>';
	}

	return $data;
}

?>


<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>სამუშაო ბლანკი</title>
	<style type="text/css">
		body{
			margin:20px;
		}

		.job-blank{
			width: 800px;
		}

		.job-blank td{
			text-align: center;
			font-weight: bold;
			font-family: Sylfaen, Arial, sans-serif;
			font-size: 10.0pt;
		}

		.job-blank .rotate{
			writing-mode: tb-rl;
			-webkit-transform: rotate(270deg);
			-moz-transform: rotate(270deg);
			-o-transform: rotate(270deg);
			white-space: nowrap;
			display: block;
			width: 16px;
		}

		.style1{
			margin-top: 45px;
			margin-left: 4px;
		}

		.style2{
			margin-top: 80px;
			margin-left: 4px;
		}

		.style3{
			margin-top: 55px;
			margin-left: 4px;
		}

		.style4{
			margin-top: 70px;
			margin-left: 4px;
		}

		.style5{
			margin-top: 80px;
			margin-left: 1px;
		}

		.sub{
			width: 25px;
		}
		.subsub{
			width: 40px;
		}
	</style>
</head>

<body>
	<table border="0" class="job-blank" cellpadding="0" cellspacing="0">
		<tr>
			<td rowspan="4" valign="top" style="padding: 2px;"><img src="logo.jpg" width="68" height="68" style="float: left;"></td>
			<td style="font-size: 12pt; padding-top: 15px;">წარმოების ბლანკი</td>
			<td colspan="2" style="font-weight: normal; padding-top: 5px; text-align: right;">შეკვეთა № :<span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 10px;"><?php echo 'R –  '.$rResult['id']; ?></span></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td rowspan="2" style="font-size: 14pt; padding-top: 10px;"><?php echo  $rResult['planned_end_date']; ?></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td style="font-weight: normal; text-align: right;">შესრულების გეგმიური დრო</td>
		</tr>
		<tr>
			<td colspan="2" >&nbsp;</td>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" style="font-weight: normal; text-align: right;">მომსახურების ობიექტი:</td>
			<td colspan="2" style="font-size: 10pt; text-align: left;"><span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;"><?php echo  $rResult['client']; ?></span></td>
		</tr>
		<tr>
			<td colspan="2" style="font-weight: normal; text-align: right;">ტესტერი:</td>
			<td colspan="2"><?php echo $rResult1['name']; ?></td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
			<td colspan="2">&nbsp;</td>
		</tr>
	</table>

	<table border="1" class="job-blank" cellpadding="0" cellspacing="0">
		<tr>
			<td rowspan="2" style="width: 22px">№</td>
			<td colspan="2">კარტრიჯი</td>
			<td colspan="9">ჩატარებული სამუშაოები</td>
			<td rowspan="2" style="width: 20px; border:1px solid black;">შემსრულებელი</td>
		</tr>
		<tr>
			<td rowspan="1"  style="height: 70px; width: 130px;"><span>ნომერი</span></td>
			<td rowspan="1" style="width: 120px;"><span>მოდელი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style1">ტონერი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style1">ბარაბანი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style2">მაგნიტ. ლილ.</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style2">რეზინის PCR</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style2">გამწ. რაკელი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style2">დოზ. რაკელი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style1">ჩიპი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style1">კორპუსი</span></td>
			<td rowspan="1" class="sub" ><span class="rotate style2">სერვ. გარეშე</span></td>
		</tr>
		<?php echo print1($rResult);?>

</body>

</html>